package com.example.project3.aop;

import com.example.project3.util.API;
import com.example.project3.util.ApiToken;
import com.example.project3.util.ThreadLocalToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.BeanUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author xh169
 * @version 1.0
 * @description: 刷新令牌
 * @date 2023/6/1 19:45
 */
@Order(1)
@Aspect
@Component
@Slf4j
public class TokenAop {
    private final ThreadLocalToken threadLocalToken;

    public TokenAop(ThreadLocalToken threadLocalToken) {
        this.threadLocalToken = threadLocalToken;
    }

    @Pointcut("execution(public * com.example.project3.controller.*Controller.*(..))")
    public void point() {
    }

    @Around("point()")
    public Object aroundToken(ProceedingJoinPoint pjp) throws Throwable {
        API proceed = (API) pjp.proceed();
        //获取token
        String token = threadLocalToken.getToken();
        if (StringUtils.isNoneBlank(token)) {
            ApiToken apiToken = new ApiToken();
            BeanUtils.copyProperties(proceed, apiToken);
            apiToken.setToken(token);
            threadLocalToken.clear();
            return apiToken;
        }
        return proceed;
    }

}

